<template>
	<view class="content">
		<view class="pay" :class="{active: index == 1}" @tap="index = 1">
			<text class="pay_left">置顶1天</text>
			<text class="pay_right">10元</text>
		</view>
		<view class="pay" :class="{active: index == 2}" @tap="index = 2">
			<text class="pay_left">置顶3天</text>
			<text class="pay_right">30元</text>
		</view>
		<view class="pay" :class="{active: index == 3}" @tap="index = 3">
			<text class="pay_left">置顶7天</text>
			<text class="pay_right">70元</text>
		</view>
		<view class="submit_btn" @tap="toPay">确认支付</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				index: 1,
				id: '',
				type: ''
			}
		},
		onLoad(option) {
			console.log(option)
			this.id = option.id
			this.type = String(option.type)
			console.log(this.type)
			console.log(this.id)
		},
		methods: {
			toPay(){
				let that = this
				let money
				if(that.index == 1){
					money = 10
				}else if(that.index == 2){
					money = 30
				}else if(that.index == 3){
					money = 70
				}
				// 先调后台接口 
				that.$api.request('/api/pay/toPay','',{
					userId: uni.getStorageSync("userId"),
					money: money * 100,
					openId: uni.getStorageSync('openid'),
					type: 'wxpay',
					topId: that.id,
					contentType: that.type,
				}, failres => {
					
				}).then(res => {
					console.log("调用的支付接口");
					let payInfo = res.data
					//获取到支付信息，调起支付
					uni.requestPayment({ //下面参数为必传
						appId: payInfo.appId, //小程序Appid
						timeStamp: payInfo.timeStamp, //创建订单时间戳
						nonceStr: payInfo.nonceStr,
						package:  payInfo.package, // 订单包
						signType: payInfo.signType, // 加密方式统一'MD5'
						paySign: payInfo.sign, // 后台支付签名返回
						success(res) { 
							// 调用置顶接口
							let topType
							let rentItemId
							let recruitMentJobId
							if(that.type == '2'){ //房源
								topType = 0
								rentItemId = that.id
							}else if(that.type == '1'){ //求职
								topType = 2
								recruitMentJobId = that.id
							}else if(that.type == '0'){ //招聘
								topType = 1
								recruitMentJobId = that.id
							}
							let params = {}
							if(that.type == 2){
								params = {
									addUserid: uni.getStorageSync("userId"),
									type: topType,
									rentItemId: rentItemId,
									status: 6,
									day: money/10
								}
							}else{
								params = {
									addUserid: uni.getStorageSync("userId"),
									type: topType,
									recruitMentJobId: recruitMentJobId,
									status: 6,
									day: money/10
								}
							}
							that.$api.request('/api/web/top','',params, failres => {
								uni.showToast({
									title: failres.msg,
									icon: "none"
								});
							}).then(res => {
								console.log(res)
								uni.showToast({
									title: "置顶成功！",
									icon: "none"
								});
								uni.navigateTo({
									url: '../fangyuan/fangyuan'
								})
							})
						},
						fail(err) {
							
						}
					})
				})
			}
			
		}
	}
</script>

<style lang="scss">
	.content{
		width: 100%;
		background: #F5F5F5;
		min-height: 100vh;
		.submit_btn{
			width: 100%;
			height: 100rpx;
			background: #0091FF;
			color: #FFFFFF;
			line-height: 100rpx;
			text-align: center;
			position: fixed;
			bottom: 0;
			left: 0;
		}
		.pay{
			width: 95%;
			height: 120rpx;
			background: #ffffff;
			margin: 20rpx auto 0 auto;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-radius: 10rpx;
			.pay_left{
				color: #333333;
				font-size: 28rpx;
				margin-left: 30rpx;
			}
			.pay_right{
				color: #FA6400;
				font-size: 32rpx;
				margin-right: 30rpx;
			}
		}
		.active{
			border: 5rpx solid #FA6400;
		}
	}
</style>
